/*
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.beiuu.myfirstandroidapp.Tab;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.beiuu.myfirstandroidapp.Bean.News;
import com.beiuu.myfirstandroidapp.Bean.NewsAdapter;
import com.beiuu.myfirstandroidapp.R;
import com.beiuu.myfirstandroidapp.Refresh.HeaderAndFooterWrapper;
import com.shizhefei.fragment.LazyFragment;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;


public class PullFragment extends LazyFragment {

    private List<News> newslist = new ArrayList<News>();
    private RecyclerView rv;
    private SuperRecyclerView superRecyclerView;
    private Context context;

    public PullFragment() {
        super();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    protected View getPreviewLayout(LayoutInflater inflater, ViewGroup container) {
        return super.getPreviewLayout(inflater, container);
    }

    @Override
    protected void onFragmentStartLazy() {
        super.onFragmentStartLazy();
    }

    @Override
    protected void onFragmentStopLazy() {
        super.onFragmentStopLazy();
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_pull);

        context = getApplicationContext() ;
        initNews();

        superRecyclerView = (SuperRecyclerView) findViewById(R.id.recyc2);
        //rv = (RecyclerView) findViewById(R.id.recyc2);
        //LinearLayoutManager lm = new LinearLayoutManager(getApplicationContext());
        //rv.setLayoutManager(lm);

        NewsAdapter Na = new NewsAdapter(getApplicationContext(),newslist);
        //rv.setAdapter(Na);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        superRecyclerView.setLayoutManager(layoutManager);
        superRecyclerView.setRefreshEnabled(true);//可以定制是否开启下拉刷新
        superRecyclerView.setLoadMoreEnabled(true);
        superRecyclerView.setAdapter(Na);

        superRecyclerView.setLoadingListener(new SuperRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //refresh data here
                initNews();

                NewsAdapter Na = new NewsAdapter(context,newslist);
                superRecyclerView.setAdapter(Na);

                superRecyclerView.completeRefresh();
            }

            @Override
            public void onLoadMore() {
                // load more data here
                superRecyclerView.completeLoadMore();
            }
        });


    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
    }

    @Override
    protected void onPauseLazy() {
        super.onPauseLazy();
    }

    @Override
    protected void onDestroyViewLazy() {
        super.onDestroyViewLazy();
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
    }

    private void initNews(){

        for (int i=0;i<20;i++){
            News f = new News();
            f.setTitle("pull标题标题标题标题标题,标题标题标题标题标题标题标题标题,标题标题标题标"+i);
            f.setImage("http://p1.pstatp.com/origin/6ecb000bd1d1f43912a3");
            f.setUrl("url"+i);
            newslist.add(f);
        }

    }
}
